#include "fxcc/core/graph/common/ShaderImporter.h"
//#include "e2d/ui/ShaderImporter.h"

ShaderMetaCacheFile ShaderImporter::m_CacheFiles;
std::string ShaderImporter::m_CacheFilePath = ".metamap/shader.metamap";
std::string ShaderImporter::m_GlslDir = "GLSL";

bool ShaderImporter::ImportFile(const fxcc::File& file)
{
	switch (file.m_FileType)
	{
	case fxcc::FileType::_Frag_:
		return ImportFragCode(file);
	default:
		return false;
	}

};


bool ShaderImporter::ImportFragCode(const fxcc::File& file)
{
	auto& m_MetaCache = m_CacheFiles.m_Caches[file.m_RelativePath];
	m_MetaCache = {};

	const std::string fragFile = file.m_RelativePath;

	ztclog::info("fragFile %s", fragFile.c_str());
	fs::path _dir = fs::path(m_LibraryPath) / m_GlslDir;

	MetaMapIndex metaMapIndex;
	metaMapIndex.SetName(file.m_OriginName);
	metaMapIndex.SetFrag(_dir.string(), fragFile, 0);

	if (fs::exists(metaMapIndex.m_TargetFilePath))
	{
		fs::remove(metaMapIndex.m_TargetFilePath);
	}
	fxcc::Base::CreateParentDirs(metaMapIndex.m_TargetFilePath);

	ztclog::info("copy %s ->  %s", fragFile.c_str(), metaMapIndex.m_TargetFilePath.c_str());
	fs::copy(fragFile, metaMapIndex.m_TargetFilePath);
	
	m_MetaCache.m_FragMetas.push_back(metaMapIndex);
	return true;
}

bool ShaderImporter::LoadCache()
{
	fxcc::yml::Serializer serializer;
	FXCC_FAILED(serializer.DecodePath<ShaderMetaCacheFile>(m_CacheFilePath, m_CacheFiles));
	return true;
}

bool ShaderImporter::SaveCache()
{
	fxcc::yml::Serializer serializer;
	FXCC_FAILED(serializer.EncodePath<ShaderMetaCacheFile>(m_CacheFilePath, m_CacheFiles));
	return true;
}
